package com.xuecheng.system.config;
 
 
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
 
/**
 * MD5加密工具类
 */
public class MD5Util {
    private static final char hexDigits[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
            'A', 'B', 'C', 'D', 'E', 'F'};
 
    /**
     * MD5加密 (大写)
     *
     * @param inStr
     * @return 32byte MD5 Value
     */
    public static String MD5(String inStr) {
        byte[] inStrBytes = inStr.getBytes();
        try {
            MessageDigest MD = MessageDigest.getInstance("MD5");
            MD.update(inStrBytes);
            byte[] mdByte = MD.digest();
            char[] str = new char[mdByte.length * 2];
            int k = 0;
            for (int i = 0; i < mdByte.length; i++) {
                byte temp = mdByte[i];
                str[k++] = hexDigits[temp >>> 4 & 0xf];
                str[k++] = hexDigits[temp & 0xf];
            }
            return new String(str);
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return null;
    }
 
    /**
     * MD5加密(小写)
     *
     * @param source
     * @return
     */
    public static String md5(String source) {
 
        StringBuffer sb = new StringBuffer(32);
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            byte[] array = md.digest(source.getBytes("utf-8"));
            for (int i = 0; i < array.length; i++) {
                sb.append(Integer.toHexString((array[i] & 0xFF) | 0x100).substring(1, 3));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
 
        return sb.toString();
    }
 
  /*  public static void main(String[] args) {
        String str ="123";
        System.out.println(MD5Util.MD5(str));
        System.out.println(MD5Util.md5(str));
    }*/
}
